<?php
/**
  * 系統名稱: Platform 系統
  * 檔案說明: 專案管理-修改
  * $Author$
  * $Id$
  *
  */

if (!defined("UserGroup_lib_include")){
	define("UserGroup_lib_include", 1);
	include_once(__Modules_Path."/LogRecord.lib.php");

/**
 * 取得管理群組資料
 * @param	intID 序號
 * @return	false -> 錯誤，object -> 成功, 
 */
	function getObjUrGroupData($intID){
		global $_Conn, $$_Conn;

		$strSQL = "select * from `bet_MemberGroup` where ID = %d";
		$strSQL = sprintf($strSQL, $intID);
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Group Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return object;
		return PMA_mysql_fetch_object($RS);
	}

/**
 * 刪除一筆管理群組資料
 * @param	intID 序號
 * @return	false -> 錯誤，int -> 成功, 
 */
	function rmUrGroup($intID){
		global $_ConnMaster;

		if(!DeleteData_Log($_SESSION["Admin_Info"] -> ID, "bet_MemberGroup", $intID))return false;

		$strSQL = "delete from `bet_MemberGroup` where `ID` = %d";
		$strSQL = sprintf($strSQL, $intID);

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Remove Group Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		return true;
	}


/**
 * 新增一筆管理群組資料
 * @param	arrGroup 語言群組名稱陣列
 * @param	arrPgID 程式序號
 * @param	arrPPower 程式權限
 * @param	arrPgPInfo 程式 Info 權限
 * @return	false -> 錯誤，int -> 成功, 
 */
	function setAddUrGroup($arrGroup, $arrPgID, $arrPPower, $arrPgPInfo){
		global $_ConnMaster;

		$strPgID = implode(",", $arrPgID);
		$strPPower = implode(",", $arrPPower);

		$strLog = "";
		$arrF = array();
		$arrV = array();
		foreach($arrGroup as $intLang => $strGroup){
			$arrF[] = "`GroupName_".$intLang."`";
			$arrV[] = "'".$strGroup."'";
			$strLog .= "GroupName_".$intLang." => ".$strGroup."\n";
		}
		$strSQL = "insert into `bet_MemberGroup`(`ProgramID`, `ProgramPower`, ".join(", ", $arrF).") ";
		$strSQL .= "values('".$strPgID."', '".$strPPower."', ".join(", ", $arrV).")";

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Add Group Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		$intNewID = mysql_insert_id($_ConnMaster);

		$strLog = "ID => ".$intNewID."\n".$strLog;
		$strLog .= "ProgramID => ".$strPgID."\n";
		$strLog .= "ProgramPower => ".$strPPower."\n";

		AddData_Log($_SESSION["User_Info"] -> ID, "新增群組管理完成", $strLog);
		return $intNewID;
	}


/**
 * 修改一管理者群組資料
 * @param	intID 序號
 * @param	arrGroup 語言群組名稱陣列
 * @param	arrPgID 程式序號
 * @param	arrPPower 程式權限
 * @param	arrPgPInfo 程式 Info 權限
 * @return	false -> 錯誤，true -> 成功, 
 */
	function setUrGroup($intID, $arrGroup, $arrPgID, $arrPPower, $arrPgPInfo){
		global $_ConnMaster;

		//先取消該群組所有權限
		//if(!setUrGroupNoPower($intID))return false;
		//if(!setUrGroupInfo($intID, $arrPgID, $arrPgPInfo))return false;

		$strPgID = implode(",", $arrPgID);
		$strPPower = implode(",", $arrPPower);

		$strLog = "";
		$strSQL = "update `bet_MemberGroup` set ";
		foreach($arrGroup as $intLang => $strGroup){
			$strSQL .= "`GroupName_".$intLang."` = '".$strGroup."', ";
			$strLog .= "GroupName_".$intLang." => ".$strGroup."\n";
		}
		$strSQL .= "`ProgramID` = '".$strPgID."', ".
				   "`ProgramPower` = '".$strPPower."' ".
				   "where ID = ".$intID;

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Edit Group Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}

		$strLog = "ID => ".$intID."\n".$strLog;
		$strLog .= "ProgramID => ".$strPgID."\n";
		$strLog .= "ProgramPower => ".$strPPower."\n";

		AddData_Log($_SESSION["User_Info"] -> ID, "修改群組管理完成", $strLog);

		//if(!setChangeLangName($intID, "wg_UserGroup", $arrGroup))return false;
		return true;
	}


/**
 * 產生使用者群組下拉清單
 * @param	intGroupID 指定群組序號
 * @param	strNotInGroup 去取得不包含群組序號集合
 * @param	strFieldName select tag 的name
 * @param	onChange onchange 時，要執行的事件
 * @return	成功傳回 String
 */
	function getManagerGroupSelectTag($intGroupID, $strNotInGroup, $strFieldName, $onChange){
		global $_Conn, $$_Conn;
		$strSQL = "select ID, `GroupName_"._LangCode."` as GroupName from `bet_MemberGroup` and ID not in (".$strNotInGroup.") and ID > 30";
		//print $strSQL;
		if(empty($onChange))$onChange="void(0)";

		$strCon = "<select name=\"".$strFieldName."\" onChange=\"".$onChange."\">\n";
		$strCon .= "<option value=\"\">--select--</option>\n";
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS)){
				$strCon .= "<option value=\"".$Rows -> ID."\"".(($intGroupID == $Rows -> ID)?" selected":"").">".$Rows -> GroupName."</option>\n";
			}
		}
		$strCon .= "</select>";
		return $strCon;
	}
//-------------------------------------------------------//
//UserGroup_lib End
}
?>